package com.moduls.data.common;

import java.io.IOException;
import java.sql.SQLException;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.mock.http.client.MockClientHttpResponse;

/**
 * huangye88 数据
 * @author gengek
 *
 */
public class Huangye88Util {

	public static void Huangye88data() throws IOException {
		
		Document document = PsUtil.getElement("http://b2b.huangye88.com/region/");
		
        Element clist = document.getElementById("clist");
        Elements aElements = clist.getElementsByTag("a");
        int i = 1;
        for (Element element:aElements) {
        	if (i==1) {
            	System.out.println(element.attr("href"));
            	Document href = PsUtil.getElement(element.attr("href"));
            	
            	//<div class="main">
            	int a = 1;
            	Elements div_mains = href.select("div[class=main]");
            	Elements div_mains_as = div_mains.select("a");
            	
            	for (Element e2 : div_mains_as) {
            		//页数访问参数
                	int count = 0;
                	int page = 0;
                	
            		Document doc_list = null;
            		if (a==1) { //用它算出总数
            			doc_list = PsUtil.getElement(e2.attr("href"));
            			//先拿页数 算出多少页
            			//总条数
            			Elements abs =doc_list.select("span[style=float:right;padding-right:10px]");
            			count = Integer.valueOf(abs.select("em").text());
            			//总页数
            			page = count%20 > 0?count/20+1:count/20;
					}
            		
            		for (int j = 1; j < 2; j++) {
            			Elements phone_url = null;
						if (j > 1) { //第一页默认url
							phone_url = doc_list.select("span[itemprop=tel]");
						} else { //其他页数据
							phone_url = PsUtil.getElement(e2.attr("href")+"pn"+j).select("span[itemprop=tel]");
						}
						
						for (Element element_contact : phone_url) {
	        				//详细页面的地址
							String contact_url = element_contact.select("a").attr("href");
	        				//获取详细页面数据
							getInfo(contact_url);
						}
					}
        			
            		a=2;
				}
            	
			}
            i++;

        }
	}
	
	//获取详细页数据
	public static void getInfo(String contact_url) throws IOException {
		Document document = PsUtil.getElement(contact_url);
		Elements div = document.select("div[class=site]");
		Elements lis = div.select("li");
		for (Element element : lis) {
			String label = element.select("label").html();
			if (label.equals("联系人：")) {
				System.out.println("联系人："+label);
			}
			if (label.equals("手机：")) {
				System.out.println("手机："+label);
			}
			if (label.equals("公司名称：")) {
				System.out.println("公司名称："+label);
			}
			if (label.equals("地址：")) {
				System.out.println("地址："+label);
			}
			if (label.equals("邮编：")) {
				System.out.println("邮编："+label);
			}
			if (label.equals("公司主页：")) {
				System.out.println("公司主页："+label);
			}
			
		}
		System.out.println("---------");
		
	}
	
	public static void main(String[] args) throws IOException, SQLException{
		Huangye88Util huangye88Util = new Huangye88Util();
		huangye88Util.Huangye88data();
	}
	
}
